package com.deyinet.bdcms.service.impl;


import com.deyinet.bdcms.core.dao.Dao;
import com.deyinet.bdcms.core.service.impl.BaseServiceImpl;
import com.deyinet.bdcms.core.vo.Page;
import com.deyinet.bdcms.dao.IDevicedataDao;
import com.deyinet.bdcms.entity.Devicedata;
import com.deyinet.bdcms.service.IDevicedataService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Bacel on 2017/05/02.
 */
@Service
public class DevicedataService extends BaseServiceImpl<Devicedata> implements IDevicedataService {

    @Autowired
    private IDevicedataDao moduleDao;

    @Override
    protected Dao getDao() {
        return moduleDao;
    }

    @Override
    public void findByExampleLike(Devicedata example, Page<Devicedata> page, Timestamp beginTime, Timestamp endTime) {
        List<Devicedata> list=moduleDao.findByExampleLike(example,page.getPageCurrent(),page.getPageSize(),beginTime,endTime);
        long count = moduleDao.countByExampleLike(example,beginTime,endTime);
        page.setList(list);
        page.setTotalRow(Long.valueOf(count));
    }



    @Override
    public void getdata(Devicedata query, Page page) {
        List<Devicedata> list = this.getDao().findByExampleEq(query, page.getPageCurrent(), page.getPageSize());
        long count = this.getDao().countByExampleEq(query);
        for (Devicedata devicedata : list) {
            if (devicedata.getMetadata() != null) {
                String data = getdataString(devicedata.getMetadata());
                devicedata.setDataString(data);
            }
        }
        page.setTotalRow(Long.valueOf(count));
        page.setList(list);
    }

    private static String getdataString(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        for (byte b : bytes) {
            stringBuffer.append((char) b);
        }
        return stringBuffer.toString();
    }

    @Override
    public List<Devicedata> getlinechartdata(String sn, Timestamp startTime, Timestamp endTime) {
        return moduleDao.getlinechartdata(sn, startTime, endTime);
    }


    @Override
    public List<Devicedata> findByExampleLikeImport(Devicedata example, Timestamp beginTime, Timestamp endTime) {
        return moduleDao.findByExampleLikeImport(example, beginTime, endTime);
    }


    @Override
    public Map<String, Integer> statistics(Timestamp start, Timestamp end) {
        Map<String, Integer> map=new HashedMap();
        map.put("deviceNormal",moduleDao.countDeviceNormal(start,end,1));
        int i2=moduleDao.countDeviceNormal(start,end,2);
        int i3=moduleDao.countDeviceNormal(start,end,3);
        int i4=moduleDao.countDeviceNormal(start,end,4);
        map.put("deviceAbnormal",i2+i3+i4);
        return map;
    }
}